var utils = require('../../utils/my_utils')
import { deparam } from '../../utils/deparam'
import base64 from '../../utils/base64'
const meetingSalt = getApp().data.meetingSalt

Page({
  data: {
    loading: true,
    isScan: false,
  },
  onLoad(options) {
    try {
      let url = decodeURIComponent(options.q)
      let param = deparam(url.split('?')[1])
      // let testParam = 'https://sichuangyi.demopp.in/signup?id=103'.split('?')[1]
      // let testID = deparam(testParam).id
      let { id } = param
      if (!id) {
        wx.switchTab({
          url: '/pages/Activity/index'
        })
        throw new Error('id not found')
      }
      let meetingID = base64.decode(id)
      
      meetingID = meetingID && meetingID.split(meetingSalt)[1]

      this.signUp(meetingID)
    } catch (e) {
      wx.showToast({
        title: '未发现会议信息，请核对二维码信息',
        icon: 'error',
        image: "/icon/error.png",
        duration: 2000
      })
      console.log(e)
    }
  },
  signUp(meeting) {
    let self = this
    if (!meeting) {
      return
    }
    utils.my_request({
      url: 'https://sichuangyi.demopp.in/api-conference-alpha/sign-up',
      method: 'POST',
      data: {
        meeting
      },
      success(resp) {
        wx.redirectTo({
          url: '/pages/ActivityDetail/index?id=' + meeting
        })
      },
      error: function (err) {
        throw new Error(err)
      }
    })
  }
})